<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box-1 {
            background-color: darkturquoise;
            height: 30rem;
            display: flex;
            justify-content: center;
            align-items: center;
        }
        .box-2 {
            background-color: white;
            display: flex;
            flex-direction: row;
            height: 3rem;
            border-radius: 1.5rem;
            justify-content: center;
            align-items: center;
            padding: 0 1rem;
            gap: 0 1rem;
            /* https://developer.mozilla.org/zh-CN/docs/Web/CSS/box-shadow */
            /* x 偏移量 | y 偏移量 | 阴影模糊半径 | 阴影扩散半径 | 阴影颜色 */
            box-shadow: 2px 2px 5px 2px rgba(0, 0, 0, 0.3);
        }
        .box-btn {
            height: 2rem;
            width: 2rem;
            display: flex;
            justify-content: center;
            align-items: center;
            border-radius: 2rem;
        }
        .box-btn-direction:hover {
            background-color: gainsboro;
            border: 0cap;
        }
        @keyframes color-white-to-black {
            from {
                background-color: white;
                color: black;
            }
            to {
                background-color: black;
                color: white;
            }
        }
        .box-btn-num-select:hover {
            animation-duration: 1s;
            animation-name: color-white-to-black;
            animation-fill-mode: forwards;
        }
        .box-btn-num-out {
            animation-duration: 1s;
            animation-name: color-white-to-black;
            animation-fill-mode: forwards;
            animation-direction: alternate-reverse;
        }
    </style>
</head>
<body>
    <div class="box-1">
        <div class="box-2" id="box-2">
        </div>
    </div>
</body>
<script>
    const content_body = document.getElementById("box-2");
    content_body.innerHTML += `<div class="box-btn box-btn-direction">←</div>`;
    for (let index = 1; index < 6; index++) {
        let element = `<div id="btn-${index}" class="box-btn box-btn-num-select" onclick="btnClick(${index})">${index}</div>`;
        content_body.innerHTML += element;
    }
    content_body.innerHTML += `<div class="box-btn  box-btn-direction">→</div>`;

    let currentIndex = -1;
    function btnClick(a) {
        console.log(a, currentIndex);
        if (currentIndex === a) {
            return;
        }
        if (currentIndex !== -1) {
            const current_node = document.getElementById(`btn-${currentIndex}`);
            current_node.className += " box-btn-num-out";
            current_node.style = ``;
            setTimeout(function() {
                current_node.className = "box-btn box-btn-num-select";
            }, 1000)
        }
        currentIndex = a;
        const select_node = document.getElementById(`btn-${a}`);
        select_node.className = "box-btn box-btn-num-select";
        select_node.style.color = 'white';
        select_node.style.backgroundColor = 'black';
    }
</script>
</html>